package stark.app.function.cartoon;

import android.graphics.Bitmap;
import android.net.Uri;
import android.view.ViewGroup;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.stark.cartoonutil.lib.BaseController;
import com.stark.cartoonutil.lib.CartoonsController;
import com.stark.cartoonutil.lib.utils.ImgUtil;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.List;

import stark.app.R;
import stark.app.databinding.FragmentCartoonImg1Binding;
import stark.common.basic.base.BaseNoModelFragment;

/**
 * Author：daizhongbin on 2023/4/27
 * Email：1027154338@qq.com
 * Desc:
 */
public class CartoonImgFragment1 extends BaseNoModelFragment<FragmentCartoonImg1Binding> {

    private int defWidth = -1;
    private int defHeight = -1;
    private CartoonsController cartoonsController;
    //是否动漫化了
    private boolean isCartooned = false;
    //是否设置了图片
    private boolean isSetImg = false;

    @Override
    protected int onCreate() {
        return R.layout.fragment_cartoon_img1;
    }

    @Override
    protected void initView() {
        mDataBinding.btnSelImg.setOnClickListener(v -> {
            selImg();
        });
        mDataBinding.btnCartoon.setOnClickListener(v -> {
            cartoonImg();
        });
        mDataBinding.btnSaveImg.setOnClickListener(v -> {
            saveImg();
        });
    }

    @Override
    protected void initData() {
        //该控制器用于对图片进行动漫化处理
        cartoonsController = new CartoonsController(getContext(), mDataBinding.glImgView);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //这里要释放资源
        if (cartoonsController != null) {
            cartoonsController.release();
            cartoonsController = null;
        }
    }

    private void selImg() {
        PhotoSelectorBuilder.builder(getActivity())
                .mode(PhotoConfig.Mode.PHOTO)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        if (data == null || data.size() == 0) {
                            return;
                        }
                        String imgUri = data.get(0).getUri();
                        if (defWidth < 0) {
                            defWidth = mDataBinding.glImgView.getWidth();
                            defHeight = mDataBinding.glImgView.getHeight();
                        }
                        Bitmap bitmap = ImgUtil.Companion.getCompressedBitmapByUri(imgUri, defWidth, defHeight);
                        ViewGroup.LayoutParams layoutParams = mDataBinding.glImgView.getLayoutParams();
                        layoutParams.width = bitmap.getWidth();
                        layoutParams.height = bitmap.getHeight();
                        mDataBinding.glImgView.setLayoutParams(layoutParams);
                        mDataBinding.glImgView.setImageSrc(bitmap.copy(bitmap.getConfig(), bitmap.isMutable()), true);
                        isCartooned = false;
                        isSetImg = true;
                    }
                });
    }

    private void cartoonImg() {
        if (!isSetImg) {
            ToastUtils.showShort("请先设置图片");
            return;
        }
        if (isCartooned) {
            ToastUtils.showShort("该图片已经过动漫化处理");
            return;
        }
        showDialog("处理中...");
        cartoonsController.applyCartoonsModel(() -> {
            isCartooned = true;
            dismissDialog();
        });
    }

    private void saveImg() {
        if (!isSetImg) {
            ToastUtils.showShort("请先设置图片");
            return;
        }
        if (!isCartooned) {
            ToastUtils.showShort("该图片还未进行动漫化处理");
            return;
        }
        showDialog("保存中...");
        float aspectRatio = mDataBinding.glImgView.getWidth() * 1f / mDataBinding.glImgView.getHeight();
        cartoonsController.save(aspectRatio, new BaseController.ISaveCallback() {
            @Override
            public void onResult(@Nullable Uri fileUri) {
                dismissDialog();
                ToastUtils.showShort(fileUri != null ? "保存成功" : "保存失败");
            }
        });
    }
}
